import http from '@/util/axios'
import type { Res, TPage } from '../types'
import * as T from './userManagement_type'

export enum ApiPath {
  List = '/api/system/user/list', // 用户列表
  CreateUser = '/api/system/user/create', // 创建用户
  UpdateUser = '/api/system/user/update', // 创建用户
  GetUserInfo = '/api/system/user/detail', // 获取用户信息
  DeleteUser = '/api/system/user/delete', // 删除用户
  UserRoles = '/api/system/user/role/list', // 用户角色列表
  UserBindRoles = '/api/system/user/role/create', // 用户绑定角色

  RoleList = '/api/system/role/list', // 角色列表
  RoleMenus = '/api/system/role/menu/query', // 指定角色菜单

  SysMenuTree = '/api/system/menu/tree', // 菜单树
}



// 获得列表
export function getList(page: TPage, filter?: any, ): Promise<Res<T.TUserItem[]>> {
  const params: {[key:string]: any} = {
    ...{
      page: 1,
      page_size: 10,
    },
    ...page, ...filter
  }
  delete params.total
  const paramArr: string[] = []
  for(const key in params) {
    paramArr.push(`${key}=${params[key]}`)
  }
  return http.get(`${ApiPath.List}?${paramArr.join('&')}`, {})
}

// 创建用户
export function createUser(data: T.TUserItem): Promise<Res<any>> {
  return http.post(ApiPath.CreateUser, data)
}

// 更新用户
export function updateUser(data: T.TUserItem): Promise<Res<any>> {
  return http.post(ApiPath.UpdateUser, data)
}

// 获取用户信息
export function getUserInfo(account: string): Promise<Res<T.TUserItem>> {
  return http.get(`${ApiPath.GetUserInfo}?id=${account}`, {})
}

// 删除用户
export function deleteUser(account: string): Promise<Res<any>> {
  return http.get(`${ApiPath.DeleteUser}?id=${account}`, {})
}


// 获得角色列表
export function getRoleList(page: TPage, role_name: string = ''): Promise<Res<T.TRoleItem[]>> {
  return http.get(`${ApiPath.RoleList}?page=${page.page}&page_size=${page.page_size}&role_name=${role_name}`, {})
}

// 获得角色菜单
export function getRoleMenus(role_id: string | number): Promise<Res<T.TRoleMenuItem[]>> {
  return http.get(`${ApiPath.RoleMenus}?role_id=${role_id}`, {})
}

// 获得系统菜单树
export function getSysMenuTree(): Promise<Res<any>> {
  return http.get(ApiPath.SysMenuTree, {})
}

// 获得用户角色列表
export function getUserRoles(account: string, page: TPage, role_name = ''): Promise<Res<T.TUserRoleItem[]>> {
  return http.get(`${ApiPath.UserRoles}?page=${page.page}&page_size=${page.page_size}&account=${account}&role_name=${role_name}`, {})
}

// 用户绑定角色
export function userBindRoles(account: string, role_id: string[]): Promise<Res<any>> {
  return http.post(ApiPath.UserBindRoles, { account, role_id })
}